//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.jstl.tag.displaytag;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

public class ActivePageNumber extends BodyTagSupport {
    private static final String CLASS_NAME = ActivePageNumber.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);	

    
	public int doStartTag() throws JspException {	
		String METHOD_NAME="doStartTag";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		
		Enumeration enu = request.getParameterNames();
		StringBuffer buffer = null;

		if (enu != null) {
			String param;
			while (enu.hasMoreElements()) {
				param = (String)enu.nextElement();
				
				if (param.startsWith("d-") && param.endsWith("-p")) {
						if (buffer == null)
							buffer = new StringBuffer();
						
						buffer.append("<input type=\"hidden\" name=\"");
						buffer.append(param);
						buffer.append("\" value=\"");
						buffer.append((String)request.getParameter(param));
						buffer.append("\" />");
						buffer.append(" ");
				}
			}
		}		

		if (buffer != null) {
		
			JspWriter out = pageContext.getOut();
			
			try {
				out.write(buffer.toString());
			}
		    catch (IOException exc) { 
		    	log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "IOException", exc);
		    	throw new JspException(exc.toString());
		    }			
		}
		
		log.exiting(CLASS_NAME, METHOD_NAME);
		return (EVAL_PAGE);
	}

	
}
